<?php 
class Comment extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model("comment_model");
		$this->page_size = 15;
	}	
	public function index($page_index=0){		
		$model = $this->comment_model->get_many_by("",$this->page_size,$page_index);//tra ve articles + total_row
		$this->config->load("paging");
		$paging = $this->config->item("paging_admin");
		$paging["per_page"]		= $this->page_size;
		$paging["uri_segment"]  = 4;
		$paging["base_url"]     = site_url($this->config->item('admin_folder')."/comment/index/");
		$paging["total_rows"] 	= $model["total_row"];
		$this->pagination->initialize($paging);
		$paging = $this->pagination->create_links();
		
		$data["paging"] = $paging;
		$data["page_index"]		= $page_index;
		$data["model"] 		= $model["result"];		
		
		$this->view(config_item('admin_folder').'/comment_index', $data);
	}
	public function form($id,$page){
		
		// load model
		$this->load->model("customer_model");
		$this->load->model("product_model");
		$post = $this->input->post();
		if(!empty($post)){
			$post_created = $post["created"];
			$post_created = explode(" ",$post_created);
			
			$post_date = $post_created[0];
			$post_date = explode("/",$post_date);
			$post_date = $post_date[2]."-".$post_date[1]."-".$post_date[0];
			
			$post_time = $post_created[1];
			$post["created"] = strtotime($post_date." ".$post_time);
			$post["status"]  = 1;
			if(empty($id)){
				$post["customer_id"] = $post[$post["table"]];
				unset($post["email_receive_news"]);
				unset($post["customers"]);	
			}
			if(!empty($id)){
				$this->comment_model->update($post,$id);
				redirect("admin/comment/form/".$id);
			}else{
				$this->comment_model->insert($post);
				redirect("admin/comment/form");
			}
		}
		/*load danhs ach khach hang gán vào select*/
		$data["customer"] 		= $this->customer_model->get_customers();
		$data["email_recieve"] = $this->comment_model->get_email_recieve();
	
		
		if(!empty($data["customer"])) foreach($data["customer"] as $item){
			$list_cusomter[$item->id]  = $item->fullname;	
		}
		
		if(!empty($data["email_recieve"])) foreach($data["email_recieve"] as $item){
			$email_recieve[$item->id]  = $item->name;	
		}
		$data["customer"] 		= $list_cusomter;
		$data["email_recieve"]  = $email_recieve;
		
		/*load danh sach tất cả các sản phẩm gan vao select*/
		$data["product"]   = $this->product_model->get_all_products();
		if(!empty($data["product"])) foreach($data["product"] as $item){
			$list_product[$item->id]  = $item->name;
		}
		if(empty($post) and !empty($id)){
			$post = $this->comment_model->get_detail_info($id);
		}
		$data["id"]		= $id;
		$data["product"]	= $list_product;
		$data["datatimepiker"] = true;
		$data["post"]	   = $post;
		$data["page"]		= $page;
	
		$this->view(config_item('admin_folder').'/comment_form',$data);		
	}
	public function delete($id,$page_index){
		$this->db->where("id",$id)->delete("comment");
		redirect(config_item('admin_folder')."/comment/index/".$page_index);		
	}
}
?>